package COMP;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ConnectException;

import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.Workbook;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;

public class ExportPDF {

	private POIFSFileSystem fs;
	private String filepath;

	public void setFilePath(String path) {
		this.filepath = path;
	}

	public void ConvertTopdf1() throws ConnectException {
		OpenOfficeConnection OOconnection = new SocketOpenOfficeConnection(8100);
		OOconnection.connect();

		String new_file_name = filepath.substring(0, filepath.indexOf(".xls"))+ ".pdf";

		File inFile = new File(filepath);
		File outFile = new File(new_file_name);

		DocumentConverter PDFconverter = new OpenOfficeDocumentConverter(
				OOconnection);
		PDFconverter.convert(inFile, outFile);
		OOconnection.disconnect();
	}

	public void ConvertToPDF2() {
		
		try {
			fs = new POIFSFileSystem(new FileInputStream(filepath));
			
			HSSFWorkbook  doc = new HSSFWorkbook (fs); 
			ExcelExtractor we = new ExcelExtractor(doc);
			
			Document document = new Document();
			String new_file_name = filepath.substring(0,filepath.indexOf(".xls"))+ ".pdf";
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new_file_name)); 
			
			document.open();
			writer.setPageEmpty(true);
//			String paragraphs = we.get;  
 
//	        document.add(new Paragraph(paragraphs));  
	        
			document.close();
		} catch (DocumentException de) {

			System.err.println(de.getMessage());

		} catch (IOException ioe) {

			System.err.println(ioe.getMessage());

		}
	}
	public void ConvertTopdf3() throws ConnectException {
		//Instantiate a new workbook with Excel file path
		Workbook workbook = new Workbook();
//		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filepath));

		try {
			workbook.loadData(filepath);
//			workbook.loadData(fs);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		try {
			workbook.save(filepath.substring(0,filepath.indexOf(".xls"))+ ".pdf", FileFormatType.PDF);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
